<?php

/**
 * proposicao actions.
 *
 * @package    camaras
 * @subpackage proposicao
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class proposicaoActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->camara = Doctrine::getTable('Camara')->find($this->getUser()->getAttribute('id',null,'camara'));
    $proposicoes = Doctrine::getTable('Proposicao')->getProposicoesPorCamara($this->getUser()->getAttribute('id',null,'camara'), $so_abertas = 0);
	
	$this->pager = new sfDoctrinePager(
		'Proposicao',
		sfConfig::get('app_qtd_registros_por_pagina')
	);
	
    $this->pager->setQuery($proposicoes);
	$this->pager->setPage($request->getParameter('page', 1));
	$this->pager->init();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->proposicao = Doctrine::getTable('Proposicao')->find(array($request->getParameter('cd_proposicao')));
    $this->forward404Unless($this->proposicao);

    $this->camara = Doctrine::getTable('Camara')->find($this->getUser()->getAttribute('id',null,'camara'));
	$this->acoes = Doctrine::getTable('Acao')->getPorProposicao($request->getParameter('cd_proposicao'));
	
    $this->pager = new sfDoctrinePager(
		'Acao',
		sfConfig::get('app_qtd_registros_por_pagina')
	);

	$this->pager->setPage($request->getParameter('page', 1));
    $this->pager->setQuery($this->acoes);
	$this->pager->init();
	
	$this->getUser()->setAttribute('last_page', @$_SERVER['ORIG_PATH_INFO'], 'usr');
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->camara = Doctrine::getTable('Camara')->find($this->getUser()->getAttribute('id',null,'camara'));
	if(!$this->getUser()->getAttribute('gerente',null,'usr')){
	  $this->getUser()->setFlash('error', sfConfig::get('app_msg_cadastro_exclusivo_gerente'));
	  $this->redirect('@homepage');
	} else {
      $this->form = new ProposicaoForm();
	}
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->camara = Doctrine::getTable('Camara')->find($this->getUser()->getAttribute('id',null,'camara'));
    $this->forward404Unless($request->isMethod('post'));

    $this->form = new ProposicaoForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
	if(!$this->getUser()->getAttribute('gerente',null,'usr')){
	  $this->getUser()->setFlash('error', sfConfig::get('app_msg_cadastro_exclusivo_gerente'));
	  $this->redirect('@homepage');
	} else {
      $this->proposicao = Doctrine::getTable('Proposicao')->find(array($request->getParameter('cd_proposicao')));
      $this->forward404Unless($proposicao = Doctrine::getTable('Proposicao')->find(array($request->getParameter('cd_proposicao'))), sprintf('Object proposicao does not exist (%s).', array($request->getParameter('cd_proposicao'))));
      $this->form = new ProposicaoForm($proposicao);
	}
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($proposicao = Doctrine::getTable('Proposicao')->find(array($request->getParameter('cd_proposicao'))), sprintf('Object proposicao does not exist (%s).', array($request->getParameter('cd_proposicao'))));
    $this->form = new ProposicaoForm($proposicao);

    $this->processForm($request, $this->form);

    $this->redirect($this->getUser()->getAttribute('show_proposicoes_page',null,'usr'));
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($proposicao = Doctrine::getTable('Proposicao')->find(array($request->getParameter('cd_proposicao'))), sprintf('Object proposicao does not exist (%s).', array($request->getParameter('cd_proposicao'))));
    $proposicao->delete();

    $this->getUser()->setFlash('notice', sfConfig::get('app_msg_confirmacao'));			
    $this->redirect($this->getUser()->getAttribute('show_proposicoes_page',null,'usr'));
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()));
    if ($form->isValid())
    {
      $proposicao = $form->save();

      $this->getUser()->setFlash('notice', sfConfig::get('app_msg_confirmacao'));			
      $this->redirect($this->getUser()->getAttribute('show_proposicoes_page',null,'usr'));
    }
  }
}
